﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class DBForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRequisitionApplicationInfo();
            }
        }

        private void BindRequisitionApplicationInfo()
        {
            try
            {
                ApplicationService service = new ApplicationService();
                ApplicationForm form = service.GetApplicationFormDetailByID(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
                numberLabel.Text = RunningNumberService.GenerateRequisitionCode();
                dateLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");
                Label2.Text = form.ApplicationCode;
                Label3.Text = form.User.UserAddress;

                if (!form.ServiceStationReference.IsLoaded)
                {
                    form.ServiceStationReference.Load();
                }
                connectPersonLabel.Text = form.User.ContactPerson;
                clientComanyTextBox.Text = form.User.FullName;
                phoneLabel.Text = form.User.UserPhone + "," + form.User.UserMobilePhone;
                personTextBox.Text = ContextService.CurrentAccount.UserName;

                ApplicationProductService productService = new ApplicationProductService();
                List<ApplicationProduct> products = productService.GetProductShouldSend(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]), ContextService.CurrentAccount.UserID);//applicationForm.ApplicationProduct.ToList();

                requisitionGridView.DataSource = products; //form.ApplicationProduct.Where(p => p.Status == "维修完毕待发货" && !p.SendBackTime.HasValue);
                requisitionGridView.DataBind();

                LogisiticCompanyService logisiticCompanyService = new LogisiticCompanyService();
                WLDDL.DataSource = logisiticCompanyService.GetAllLogisticCompany();
                WLDDL.DataTextField = "Name";
                WLDDL.DataValueField = "ID";
                WLDDL.DataBind();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "DBForm.aspx::BindRequisitionApplicationInfo");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void sendLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(distributeIDTextBox.Text))
                {
                    ApplicationService applicatonservice = new ApplicationService();
                    ApplicationForm applicationForm = applicatonservice.GetApplicationFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
                    applicationForm.SendWay = "B";
                    LogisiticCompanyService lcService = new LogisiticCompanyService();
                    RequisitionForm reqForm = new RequisitionForm();
                    reqForm.RequisitionCode = numberLabel.Text;
                    reqForm.RequisitionPerson = personTextBox.Text;
                    reqForm.WayOfTransport = wayDropDownList.SelectedValue;
                    reqForm.LogisticCompany = lcService.GetLogisticCompanyByID(Convert.ToInt32(WLDDL.SelectedValue));
                    reqForm.DistributionFormID = distributeIDTextBox.Text;
                    reqForm.RequisitionDatetime = DateTime.Now;
                    ApplicationProductService productService = new ApplicationProductService();
                    List<ApplicationProduct> products = productService.GetProductShouldSend(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]), ContextService.CurrentAccount.UserID);//applicationForm.ApplicationProduct.ToList();

                    foreach (ApplicationProduct product in products)
                    {
                        if (!product.RequisitionFormReference.IsLoaded)
                        {
                            product.RequisitionFormReference.Load();
                        }

                        product.IsDiaoBo = true;

                        if (product.RequisitionForm == null)
                        {
                            product.RequisitionForm = reqForm;
                        }
                        productService.UpdateProduct(product);
                    }

                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('信息提交成功，已保存发货记录'); window.close(); window.opener.location.reload(true); ", true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('信息提交失败，请您输入物流单号！');", true);
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "DBForm.aspx -> sendLinkButton_Click");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息保存发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }
    }
}
